const router = require('koa-router')();
const corporationService = require('../service/corporationService');
const fileUtils = require('../util/fileUtils');
router.prefix('/api/corporation');

/**
 * @api {post} /api/corporation/getInformation 获取公司基本信息
 * @apiName getInformation
 * @apiGroup corporation
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：没有数据，3：服务器出错）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {json} data 获取成功时返回的数据
 */

router.post('/getInformation', async (ctx) => {
    try {
        let result;
        result = await corporationService.handleGetInformation();
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});


/**
 * @api {post} /api/corporation/editInformation 编辑公司基本信息
 * @apiName editInformation
 * @apiGroup corporation
 *
 * @apiParam {String} name 公司名.
 * @apiParam {String} address 公司地址.
 * @apiParam {String} contact 联系人.
 * @apiParam {String} phone 联系电话.
 * @apiParam {String} wechat 微信.
 *
 * @apiSuccess {int} code 返回状态码（1：修改成功，2：修改失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */
router.post('/editInformation', async (ctx) => {
    try {
        let data = ctx.request.body;
        let name = data.name,
            address = data.address,
            contact = data.contact,
            phone = data.phone,
            wechat = data.wechat;
        if ((name && name.length > 0) && (address && address.length > 0) && (contact && contact.length > 0) && (phone && phone.length > 0) && (wechat && wechat.length > 0)) {
            let result;
            result = await corporationService.handleEditInformation(data);
            ctx.body = result;
        } else {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
        }
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

module.exports = router;